package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.*;
import cn.wolfcode.wms.exception.LogicException;
import cn.wolfcode.wms.mapper.StockIncomeBillMapper;
import cn.wolfcode.wms.mapper.StockIncomebillItemMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.IProductStockService;
import cn.wolfcode.wms.service.IStockIncomeBillService;
import cn.wolfcode.wms.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

@Service
public class StockIncomeService implements IStockIncomeBillService {



    @Autowired
    private StockIncomeBillMapper stockincomebillMapper;
    @Autowired
    private StockIncomebillItemMapper stockincomebillItemMapper;
    @Autowired
    private IProductStockService productStockService;


    public int deleteByPrimaryKey(Long id) {
        return stockincomebillMapper.deleteByPrimaryKey(id);
    }

    public int insert(StockIncomeBill record) {
        return stockincomebillMapper.insert(record);
    }

    public StockIncomeBill selectByPrimaryKey(Long id) {
        return stockincomebillMapper.selectByPrimaryKey(id);
    }

    public List<StockIncomeBill> selectAll() {
        return stockincomebillMapper.selectAll();
    }

    public int updateByPrimaryKey(StockIncomeBill record) {
        return stockincomebillMapper.updateByPrimaryKey(record);
    }


    public void saveorupdate(StockIncomeBill stockincomebill) {

        if (stockincomebill.getId() != null) {
            update(stockincomebill);
        } else {
            save(stockincomebill);
        }
    }

    //具体的更新逻辑
    private void update(StockIncomeBill stockincomebill) {
        //1.获取当前用户,设置录入的相关信息
        Employee employee = UserContext.getCurrentUser();
        stockincomebill.setInputTime(new Date());
        stockincomebill.setInputUser(employee);
        //2.重置订单状态
        stockincomebill.setStatus(StockIncomeBill.STATUS_NORMAL);
        //3.计算总金额与数量

        BigDecimal totalAmount = BigDecimal.ZERO;
        BigDecimal totalNumber = BigDecimal.ZERO;
        //4.遍历订单明细
        for (StockIncomebillItem item : stockincomebill.getItems()) {
            //5.计算明细金额小计,并设置到明细中
            BigDecimal amount = item.getNumber().multiply(item.getCostPrice()).setScale(2, RoundingMode.HALF_UP);
            item.setAmount(amount);
            //6.累加总金额与总数量
            totalAmount = totalAmount.add(amount);
            totalNumber = totalNumber.add(item.getNumber());
        }
        //7.设置订单总金额和总数量
        stockincomebill.setTotalAmount(totalAmount);
        stockincomebill.setTotalNumber(totalNumber);
        //8.保存订单
        stockincomebillMapper.insert(stockincomebill);
        //9.为所有明细设置订单,并保存明细
        for (StockIncomebillItem item : stockincomebill.getItems()) {
            item.setBillId(stockincomebill.getId());
            stockincomebillItemMapper.insert(item);

        }


    }


    //具体的新增逻辑
    private void save (StockIncomeBill stockincomebill){
        StockIncomeBill sib = stockincomebillMapper.selectByPrimaryKey(stockincomebill.getId());
        if(sib != null && sib.getStatus() == StockIncomeBill.STATUS_AUDITED){

            throw  new LogicException("订单已经审核无法跟新");

        }

        //1.删除原来的明细
        stockincomebillItemMapper.deleteByBillId(stockincomebill.getId());
        //2.计算总金额与总数量
        BigDecimal totalAmount = BigDecimal.ZERO;
        BigDecimal totalNumber = BigDecimal.ZERO;
        //3.遍历明细,计算明细金额小计
        for (StockIncomebillItem item : stockincomebill.getItems()) {

            BigDecimal amount = item.getNumber().multiply(item.getCostPrice())  .setScale(2,RoundingMode.HALF_UP);
            item.setAmount(amount);
            item.setBillId(stockincomebill.getId());
            stockincomebillItemMapper.insert(item);
            //累加总金额和总数量
             totalAmount=totalAmount.add(amount);
             totalNumber = totalNumber.add(item.getNumber());

        }
        //5.设置订单总中数量
        stockincomebill.setTotalAmount(totalAmount);
        stockincomebill.setTotalNumber(totalNumber);
        //6.跟新订单
        stockincomebillMapper.updateByPrimaryKey(stockincomebill);

    }





    public void delete(Long id) {
        StockIncomeBill sib = stockincomebillMapper.selectByPrimaryKey(id);
        if(sib.getStatus() == StockIncomeBill.STATUS_AUDITED){

            throw  new LogicException("订单已经审核无法跟新");

        }
        if(id != null){
            stockincomebillItemMapper.deleteByBillId(id);
            stockincomebillMapper.deleteByPrimaryKey(id);
        }
    }

    public PageResult query(QueryObject qo) {
        int count = stockincomebillMapper.queryForCount(qo);
        if(count == 0){
            return  PageResult.empty(qo.getPageSize());
        }
        List<?> list = stockincomebillMapper.queryForList(qo);

        PageResult pageResult = new PageResult(qo.getPageSize(), count, qo.getCurrentPage(), list);


        return pageResult;
    }

    public void audit(Long id) {
        //先通过id查询当前订单
        StockIncomeBill stockincomebill = stockincomebillMapper.selectByPrimaryKey(id);
        //判断订单是否审核
        if(stockincomebill != null && StockIncomeBill.STATUS_NORMAL == stockincomebill.getStatus() ){
            //跟新审核相关信息
            stockincomebill.setAuditor(UserContext.getCurrentUser());
            stockincomebill.setAuditTime(new Date());
            stockincomebill.setStatus(StockIncomeBill.STATUS_AUDITED);
            //跟新订单
            stockincomebillMapper.autid(stockincomebill);

            //库存操作
            productStockService.stockIncome(stockincomebill);








        }
    }
}

